﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BaiduEditor.PluginInterface;
namespace FirstPlugin.UI
{
	public partial class MainForm : Form
	{
		private PluginLoaderBase loader;
		public MainForm(PluginLoaderBase loader)
		{
			this.loader = loader;
			InitializeComponent();
			Init();
		}

		private void Init()
		{
			this.StartPosition = FormStartPosition.CenterScreen;
			this.FormBorderStyle = FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;

			this.Shown += MainForm_Shown;
			treeMaterial.AfterSelect += treeMaterial_AfterSelect;
			gridMaterial.ColumnCount = 2;

			gridMaterial.Columns[0].HeaderText = "关键词";
			gridMaterial.Columns[1].HeaderText = "出价";

			gridMaterial.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			gridMaterial.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
		}

		void treeMaterial_AfterSelect(object sender, TreeViewEventArgs e)
		{
			TreeNode node = treeMaterial.SelectedNode;
			PluginAdGroup adGroup;
			try
			{
				adGroup = (PluginAdGroup)node.Tag;
				List<PluginKeyword> keywords = GetKeywords(adGroup.ID);

				SetToDataGrid(keywords);
			}
			catch
			{
				
			}
		}

		private void MainForm_Shown(object sender, EventArgs e)
		{
			SetMaterialTree();
		}

		private void SetMaterialTree()
		{
			PluginAccount account = GetAccount();
			if (account == null)
			{
				return;
			}

			TreeNode root = new TreeNode(account.Name);
			root.Tag = account;

			List<PluginCampaign> campaigns = GetCampaigns();
			if (campaigns != null)
			{
				foreach (PluginCampaign camp in campaigns)
				{
					TreeNode campNode = new TreeNode(camp.Name);
					campNode.Tag = camp;
					List<PluginAdGroup> adGroups = GetAdGroups(camp.ID);
					if (adGroups != null)
					{
						foreach (PluginAdGroup adGroup in adGroups)
						{
							TreeNode adGroupNode = new TreeNode(adGroup.Name);
							adGroupNode.Tag = adGroup;
							
							campNode.Nodes.Add(adGroupNode);
						}
					}
					root.Nodes.Add(campNode);
				}
			} 
			
			treeMaterial.Nodes.Add(root);
		}

		private PluginAccount GetAccount()
		{
			PluginGetParameter getParam = new PluginGetParameter(null);
			PluginMessage getMsg = new PluginMessage(PluginMessageCode.GetCurrentAccount, getParam);
			PluginResult result = loader.CallEditor(getMsg);

			if (result.IsSuccessful)
			{
				return (PluginAccount)result.Value;
			} 
			else
			{
				return null;
			}
		}

		private List<PluginCampaign> GetCampaigns()
		{
			PluginGetParameter getParam = new PluginGetParameter(null);
			PluginMessage getMsg = new PluginMessage(PluginMessageCode.GetCampaigns, getParam);
			PluginResult result = loader.CallEditor(getMsg);
			if (result.IsSuccessful)
			{
				return (List<PluginCampaign>)result.Value;
			}
			else
			{
				return null;
			}
		}

		private List<PluginAdGroup> GetAdGroups(long campaignId)
		{
			PluginGetParameter getParam = new PluginGetParameter(campaignId);
			PluginMessage getMsg = new PluginMessage(PluginMessageCode.GetAdGroups, getParam);
			PluginResult result = loader.CallEditor(getMsg);

			if (result.IsSuccessful)
			{
				return (List<PluginAdGroup>)result.Value;
			} 
			else
			{
				return null;
			}
		}

		private List<PluginKeyword> GetKeywords(long adGroupId)
		{
			PluginGetParameter getParam = new PluginGetParameter(adGroupId);
			PluginMessage getMsg = new PluginMessage(PluginMessageCode.GetKeywords, getParam);
			PluginResult result = loader.CallEditor(getMsg);
			if(result.IsSuccessful)
			{
				return (List<PluginKeyword>)result.Value;
			}
			else
			{
				return null;
			}
		}

		private void SetToDataGrid(List<PluginKeyword> words)
		{
			gridMaterial.Rows.Clear();
			if (words == null)
			{
				return;
			}

			foreach (PluginKeyword word in words)
			{
				gridMaterial.Rows.Add(new Object[] { word.Keyword, word.Price.ToString() });
			}
		}
	}
}
